package com.embrion.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.embrion.bean.Alunno;
import com.embrion.bean.Classe;
import com.embrion.bean.Professore;
import com.embrion.bean.Scuola;
import com.embrion.db.DataHandler;
import com.embrion.utils.Const;
import com.embrion.utils.Misc;

/**
 * Servlet implementation class Controller
 */
public class Controller extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6710833093851267826L;
	private static ServletConfig config = null;
	private static DataHandler dh = null;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String returnPage = null;
		String action = request.getParameter(Const.actionParameter);

		HttpSession session = request.getSession();
		Professore p = (Professore) session.getAttribute(Const.professoreAttr);

		if (p != null) {

			if ((action != null) && action.equalsIgnoreCase(Const.getClassi)) {

				Integer id_scuola = Integer
						.parseInt(request.getParameter("id"));

				ArrayList<Classe> classeAL = dh.getClasse(id_scuola);
				request.setAttribute(Const.listaClassi, classeAL);

				returnPage = Const.listaClassiPage;

			} else if ((action != null)
					&& action.equalsIgnoreCase(Const.getAlunni)) {

				Integer id_classe = Integer.parseInt(request
						.getParameter("id_classe"));

				ArrayList<Alunno> alunnoAL = dh.getAlunni(id_classe);
				request.setAttribute(Const.listaAlunni, alunnoAL);
				request.setAttribute(Const.id_classe, id_classe);
				returnPage = Const.listaAlunniPage;

			}
			
			
			
			//SALVATAGGIO AGGIORNAMENTO CLASSE
			else if ((action != null) && action.equalsIgnoreCase(Const.salvaAggiornaClasse)) {

				Integer id_classe = Integer.parseInt(request.getParameter("id_classe"));

				@SuppressWarnings("unchecked")
				Enumeration<String> nomiEnum = request.getParameterNames();
				
				while(nomiEnum.hasMoreElements()){
					
					String nome = nomiEnum.nextElement();
					System.out.println(nome +" - " +request.getParameter(nome) ); 
					
				}
				
				
				
				ArrayList<Alunno> alunnoAL = dh.getAlunni(id_classe);
				request.setAttribute(Const.listaAlunni, alunnoAL);
				request.setAttribute(Const.id_classe, id_classe);
				returnPage = Const.listaAlunniPage;

			}

			else {
				ArrayList<Scuola> scuolaAL = dh.getScuola(p);
				request.setAttribute(Const.listaScuole, scuolaAL);
				returnPage = Const.homePage;

			}

		} else {
			returnPage = Const.loginServlet;
		}

		Misc.gotoPage(returnPage, request, response,
				Controller.config.getServletContext());

	}

	public void init(ServletConfig _config) throws ServletException {
		if (Const.homePath == null) {
			Const.homePath = _config.getServletContext().getRealPath("/");
		}
		Controller.dh = new DataHandler();
		Controller.config = _config;
		super.init(_config);
	}

	public void destroy() {
		if (Controller.dh != null) {
			Controller.dh.destroy();
			Controller.dh = null;
		}
		super.destroy();
	}

}
